package com.xiaolong.common.context;

import com.xiaolong.common.constant.MessaggeConstant;
import com.xiaolong.common.constant.ModleTypeConstant;
import com.xiaolong.common.constant.StatusConstant;

import java.io.Serial;
import java.io.Serializable;


/**
 * @author Along
 */
public class ModelViewResponse<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 8360406821998647589L;

    private int code = 0;
    private String type;
    private String message;
    private T data;

    public ModelViewResponse(T data) {
        this.data = data;
    }

    public ModelViewResponse(int code, String type, String message, T data) {
        this.code = code;
        this.type = type;
        this.message = message;
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


    public static <T> ModelViewResponse<T> success(T data) {
        return new ModelViewResponse<>(StatusConstant.SUCCESS, ModleTypeConstant.TYPE_SUCCESS, MessaggeConstant.MSG_SUCCESS, data);
    }

    public static <T> ModelViewResponse<T> success(String message, T data) {
        message = message != null && !message.isEmpty() ? message : MessaggeConstant.MSG_SUCCESS;
        return new ModelViewResponse<>(StatusConstant.SUCCESS, ModleTypeConstant.TYPE_SUCCESS, message, data);
    }

    public static <T> ModelViewResponse<T> info(int code, String message, T data) {
        message = message != null && !message.isEmpty() ? message : MessaggeConstant.MSG_SUCCESS;
        return new ModelViewResponse<>(code, ModleTypeConstant.TYPE_INFO, message, data);
    }

    public static <T> ModelViewResponse<T> warning(int code, String message, T data) {
        message = message != null && !message.isEmpty() ? message : MessaggeConstant.MSG_WARNING;
        return new ModelViewResponse<>(code, ModleTypeConstant.TYPE_WARNING, message, data);
    }

    public static <T> ModelViewResponse<T> error(int code, String message, T data) {
        message = message != null && !message.isEmpty() ? message : MessaggeConstant.MSG_FAIL;
        return new ModelViewResponse<>(code, ModleTypeConstant.TYPE_ERROR, message, data);
    }


    public static <T> ModelViewResponse<T> error(int code, String message) {
        message = message != null && !message.isEmpty() ? message : MessaggeConstant.MSG_FAIL;
        return new ModelViewResponse<>(code, ModleTypeConstant.TYPE_ERROR, message, null);
    }

    public static <T> ModelViewResponse<T> error(String message) {
        message = message != null && !message.isEmpty() ? message : MessaggeConstant.MSG_FAIL;
        return new ModelViewResponse<>(StatusConstant.FAIL, ModleTypeConstant.TYPE_ERROR, message, null);
    }

    public static <T> ModelViewResponse<T> fail(T data) {
        return new ModelViewResponse<>(StatusConstant.FAIL, ModleTypeConstant.TYPE_ERROR, MessaggeConstant.MSG_FAIL, data);
    }

    public static <T> ModelViewResponse<T> fail(String message, T data) {
        message = message != null && !message.isEmpty() ? message : MessaggeConstant.MSG_FAIL;
        return new ModelViewResponse<>(StatusConstant.FAIL, ModleTypeConstant.TYPE_ERROR, message, data);
    }
}
